<template>
	<view class="content">
		<view class="bet" style="padding: 40rpx 0" @click="uploadImg()">
			<view class="z-text">打卡照片</view>
			<image :src="dk_avatar_text" style="width: 80rpx;height: 80rpx;border-radius: 50%;" mode=""></image>
			<!-- <image src="https://app.shiyijia.vip/uploads/20250313/a962812e2aaa88367b469a88805a72f0.jpg" style="width: 80rpx;height: 80rpx;border-radius: 50%;" mode=""></image> -->
			
		</view>
		<u-line></u-line>
		<view v-for="item in inputList" :key="item.key">
			<template v-if="item.key !='idcard'">
				<y-input :text="item.name" :inputVal="from[item.key]" @change="change" :type="item.key" :disabled="
				  isShow && item.key !== 'bank_name' && item.key !== 'bank_card'
				"></y-input>
			</template>
			<template v-if="item.key =='idcard'">
				<y-input :text="item.name" :inputVal="from[item.key]" @change="change" @bulrfn="changebir"
					:type="item.key" :disabled="
				  isShow && item.key !== 'bank_name' && item.key !== 'bank_card'
				"></y-input>
			</template>
			<u-line></u-line>
		</view>
		<!-- <view class="bet" style="padding: 40rpx 0" @click="selecttime()">
			<view class="z-text">生日</view>
			<view class="bet" style="color: #8e8e93; font-size: 24rpx">
				{{ birthday ? birthday : "请选择" }}
				<u-icon size="14" name="arrow-right" color="#8E8E93"></u-icon>
			</view>
		</view> -->
		<y-input text="生日" :inputVal="from.birlalal" :type="birlalal" :disabled="true">
			"></y-input>
		<u-line></u-line>
		<view class="bet" style="padding: 40rpx 0" @click="selectkhh()">
			<view class="z-text">开户行</view>
			<view class="bet" style="color: #8e8e93; font-size: 24rpx">
				{{ bank_name ? bank_name : "请选择" }}
				<u-icon size="14" name="arrow-right" color="#8E8E93"></u-icon>
			</view>
		</view>
		<u-line></u-line>
		<view v-for="item in inputList2" :key="item.key">
			<y-input :text="item.name" :inputVal="from[item.key]" @change="change" :type="item.key" :disabled="
		  isShow"></y-input>
			<u-line></u-line>
		</view>
		<u-line></u-line>
		<view v-for="(item, index) in selectList" :key="index" v-if="">
			<view class="bet" style="padding: 40rpx 0" @click="openModal(item)">
				<view class="z-text">{{ item.name }}</view>
				<view class="bet" style="color: #8e8e93; font-size: 24rpx">
					{{ from[item.key].name ? from[item.key].name : "请选择" }}
					<u-icon size="14" name="arrow-right" color="#8E8E93"></u-icon>
				</view>
			</view>
			<u-line></u-line>
		</view>
		<view class="bet" style="padding: 40rpx 0" @click="selectsf()">
			<view class="z-text">省份</view>
			<view class="bet" style="color: #8e8e93; font-size: 24rpx">
				{{ address ? address : "请选择" }}
				<u-icon size="14" name="arrow-right" color="#8E8E93"></u-icon>
			</view>
		</view>
		<u-line></u-line>
		<view class="bet" style="padding: 40rpx 0" @click="qiyeopen">
			<view class="z-text">企业</view>
			<view class="bet" style="color: #8e8e93; font-size: 24rpx">
				{{ company_text ? company_text : "请选择" }}
				<u-icon size="14" name="arrow-right" color="#8E8E93"></u-icon>
			</view>
		</view>
		<view style="height: 200rpx"></view>
		<view class="fabu">
			<!-- <view class="fabu" v-if="isShowEdit"> -->
			<button @click="fabu" class="button">确定</button>
		</view>
		<u-picker :show="show" :columns="columns" @confirm="select" @cancel="show = false"></u-picker>
		<u-picker :show="qyshow" :columns="qycolumns" keyName="name" @cancel="qyshow=false"
			@confirm="qyconfirm"></u-picker>
		<u-picker :show="khhshow" :columns="khhcolumns" keyName="name" @cancel="qyshow=false"
			@confirm="khhconfirm"></u-picker>
		<!-- 密码键盘 -->
		<payKeyboard v-if="showKeyBoard" title="安全键盘" @success="enterSuccess" @close="showKeyBoard=false"></payKeyboard>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<u-picker :show="shows" ref="uPicker" :columns="columns1" @confirm="confirm" @cancel="shows = false"
			@change="changeHandler"></u-picker>
	</view>
</template>

<script>
	import yInput from "@/components/y-input/index";
	const httpRequest = require("@/common/httpRequest.js")
	import payKeyboard from '@/components/mi-payKeyboard/mi-payKeyboard.vue'

	export default {
		name: "ImproveData",
		components: {
			yInput,
			payKeyboard
		},
		data() {
			return {
				show: false,
				isShowEdit: true,
				from: {
					username: {
						name: ""
					},
					idcard: {
						name: ""
					},
					urgent_username: {
						name: ""
					},
					urgent_mobile: {
						name: ""
					},
					bank_card: {
						name: ""
					},
					gender: {
						name: ""
					},
					birlalal: {
						name: ""
					},
					educationList: {
						name: ""
					},
					// provinceList: {
					// 	name: ""
					// },
					gangweiList: {
						name: ""
					},
					zhiwuList: {
						name: ""
					},
					xinzirangeList: {
						name: ""
					},
					workexperienceList: {
						name: ""
					}
				},
				inputList: [{
						name: "姓名*",
						key: "username"
					},
					{
						name: "手机号*",
						key: "mobile"
					},
					{
						name: "身份证号*",
						key: "idcard"
					},
					{
						name: "紧急联系人姓名*",
						key: "urgent_username"
					},
					{
						name: "紧急联系人手机号*",
						key: "urgent_mobile"
					},
				],
				inputList2: [{
					name: "银行卡号",
					key: "bank_card"
				}, ],
				selectList: [{
						name: "性别*",
						key: "gender"
					},
					{
						name: "学历*",
						key: "educationList"
					},
					// {
					// 	name: "省份",
					// 	key: "provinceList"
					// },
					{
						name: "岗位*",
						key: "gangweiList"
					},
					{
						name: "职务*",
						key: "zhiwuList"
					},
					{
						name: "薪资范围*",
						key: "xinzirangeList"
					},
					{
						name: "工作经验*",
						key: "workexperienceList"
					},
				],
				columnList: {
					gender: [{
							id: 0,
							name: "男"
						},
						{
							id: 1,
							name: "女"
						},
					],
				},
				columns: [],
				selects: {},
				isShow: false,
				idcard: "", //用户身份证号码，用以判断用户是否第一次完善资料
				company_id: "",
				company_text: "",
				qycolumns: [],
				qyshow: false,
				// 密码模块
				entermm: false, //是否输入过密码
				showKeyBoard: false,
				zqmm: "",
				// 开户行
				khhcolumns: [],
				khhshow: false,
				bank_id: "",
				bank_name: "",
				messageTime: {
					timeShow: false,
					title: "选择出生年月",
					mode: "date", //date日期，datetime年月日时分秒，time时间，year-month年月
					minDate: "0" //最小时间
				},
				// 省份
				shows: false,
				columns1: [],
				provinces: "",
				citys: "",
				qus: "",
				address: "",
				birthday: "",
				dk_avatar:"",
				dk_avatar_text:"",
			};
		},
		async onLoad() {
			this.getUserAuth();
			let list = await this.getfromInfo();
			this.columnList = {
				...this.columnList,
				...list
			};
			this.getqyList()
			this.getmm()
			this.getAreaList()
		},
		watch: {
			"from": {
				deep: true,
				handler(newval) {
					console.log("监听", this.from.idcard);
					if (this.from.idcard.length == 18) {
						this.from.birlalal = this.getBirthDate(this.from.idcard)
					}
				}
			}
		},
		methods: {
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = 1;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.dk_avatar = res.url
					this.dk_avatar_text = image_item
				}).catch(err => {});
			},
			getBirthDate(idCard) {
				var pattern = /^(\d{6})(\d{4})(\d{2})(\d{2})/;
				var match = pattern.exec(idCard);
				if (match) {
					var year = match[2];
					var month = match[3];
					var day = match[4];
					return year + '-' + month + '-' + day;
				}
				return null;
			},
			changebir(e) {
				this.birthday = this.getBirthdayByIdCard(e)
			},
			getBirthdayByIdCard(idCard) {
				if (idCard.length === 15) {
					return ['19' + idCard.substr(6, 2), idCard.substr(8, 2), idCard.substr(10, 2)].join('-')
				} else if (idCard.length === 18) {
					return [idCard.substr(6, 4), idCard.substr(10, 2), idCard.substr(12, 2)].join('-')
				}

				return ''
			},
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					indexs,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker,
				} = e;
				console.log(e, "eeeee=....");
				console.log(indexs, "indexs");
				const city = [];
				const county = [];
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					this.areaList[indexs[0]].children.map((item) => {
						city.push(item.name);
					});
					this.areaList[indexs[0]].children[indexs[1]].children.map((item) => {
						county.push(item.name);
					});
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, city);
					picker.setColumnValues(2, county);
				} else if (columnIndex === 1) {
					this.areaList[indexs[0]].children[indexs[1]].children.map((item) => {
						county.push(item.name);
					});
					picker.setColumnValues(2, county);
				}
			},
			async getAreaList() {
				const province = [];
				const city = [];
				const county = [];
				const a = await this.http.get("index/area"); // a.data
				this.areaList = a.data;
				a.data.map((item) => {
					province.push(item.name);
				});
				a.data[0].children.map((item) => {
					city.push(item.name);
				});
				a.data[0].children[0].children.map((item) => {
					county.push(item.name);
				});
				this.columns1 = [province, city, county];
				console.log(this.columns1, "columns1");
			},
			confirm(e) {
				this.provinces = this.areaList[e.indexs[0]].id
				this.citys = this.areaList[e.indexs[0]].children[e.indexs[1]].id
				this.qus = this.areaList[e.indexs[0]].children[e.indexs[1]].children[e.indexs[2]].id
				this.address = e.value.join('/');
				console.log(this.provinces, this.citys, this.qus);
				this.shows = false;
			},
			timeConfirm(e) {
				this.birthday = e
			},
			getmm() {
				httpRequest.request("/api/index/getConfigInfo", 'GET', {}, false, false, true).then(res => {
					this.zqmm = res.data.qy_password
				})
			},
			enterSuccess(password) {
				this.showKeyBoard = false
				console.log(password) // 输入的密码
				if (password != this.zqmm) {
					httpRequest.toast("密码不正确")
				} else {
					this.entermm = true
					this.qyshow = true
				}
			},
			qyconfirm(e) {
				this.company_id = e.value[0].id
				this.company_text = e.value[0].name
				this.qyshow = false
			},
			khhconfirm(e) {
				this.bank_id = e.value[0].id
				this.bank_name = e.value[0].name
				this.khhshow = false
			},
			getqyList() {
				httpRequest.request("/api/index/companyindex", 'GET', {}, false, false, true).then(res => {
					this.qycolumns.push(res.data)
				})
				httpRequest.request("/api/index/openbandIndex", 'GET', {}, false, false, true).then(res => {
					this.khhcolumns.push(res.data)
				})
			},
			async getUserAuth() {
				const data = await this.http.get("user/getUerInfo");
				this.isShow = !!data.data.idcard;
				this.isShowEdit = !this.isShow;
				console.log(this.isShow, "this.isShow ");
				this.dk_avatar = data.data.dk_avatar
				this.dk_avatar_text = data.data.dk_avatar_text
				this.from = {
					username: data.data.username,
					mobile: data.data.mobile,
					idcard: data.data.idcard,
					provinceList: data.data.province,
					gangweiList: data.data.gangwei,
					xinzirangeList: data.data.xinzirange,
					workexperienceList: data.data.workexperience,
					educationList: data.data.education,
					zhiwuList: data.data.zhiwu,
					urgent_username: data.data.urgent_username,
					urgent_mobile: data.data.urgent_mobile,
					// bank_name: data.data.bank_name,
					bank_card: data.data.bank_card,
					birlalal: data.data.birthday,
					gender: {
						id: 0,
						name: data.data.gender === 0 ? "男" : "女"
					},
				};
				this.address = data.data.cityinfo
				this.provinces = data.data.province_id
				this.citys = data.data.city_id
				this.qus = data.data.area_id
				this.company_id = data.data.company_id
				this.birthday = data.data.birthday
				this.company_text = data.data.company.name
				// this.bank_id = data.data.bank_id
				this.bank_name = data.data.bank_name
				console.log("完善资料页面获取用户信息")
				console.log(data)
				this.idcard = data.data.idcard;
			},
			change(e) {
				this.from[e.type] = e.val;
			},
			openModal(item) {
				if (this.isShow) return;
				this.selects = item;
				this.columns = [];
				const list = [];
				this.columnList[item.key].map((item) => {
					list.push(item.name);
				});
				this.columns.push(list);
				console.log(this.columns, "this.columns");
				this.show = true;
			},
			selectkhh() {
				if (this.isShow) return;
				this.khhshow = true
			},
			selectsf() {
				if (this.isShow) return;
				this.shows = true
			},
			selecttime() {
				// if (this.isShow) return;
				this.messageTime.timeShow = true
			},
			qiyeopen() {
				if (this.isShowEdit) {
					if (this.entermm) {
						this.qyshow = true
					} else {
						this.showKeyBoard = true
						console.log(this.showKeyBoard, "jinglei");
					}
				}

			},
			select(e) {
				this.from[this.selects.key] =
					this.columnList[this.selects.key][e.indexs[0]];
				this.show = false;
				console.log(this.from, "this.from");
			},

			PostToRecruitment() {
				//此函数是当用户信息填写完毕保存的同时自动在tab栏找工作处发送一条求职信息

				if (this.idcard == "" || this.idcard == undefined || this.idcard == "undefined" || this.idcard == null) {
					//此判断是为了只有用户是第一次完善资料时才会执行下面代码
				} else {
					return;
				};

				let params = {
					title: this.from.gangweiList.name, //岗位名称
					workexperience_id: this.from.workexperienceList ?
						this.from.workexperienceList.id : "", //工作经验年限id
					education_id: this.from.educationList ? this.from.educationList.id : "", //学历水平id
					xinzirange_id: this.from.xinzirangeList ?
						this.from.xinzirangeList.id : "", //薪资水平id
					fulilabel_ids: [],
					content: "---", //求职描述
					name: this.from.username, //用户名
					phone: this.from.mobile, //用户手机号码
					address: this.from.provinceList.name ? this.from.provinceList.name : "", //用户通信地址
				};

				console.log(params, "params");
				this.http.post("work/add", params).then((res) => {
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
						console.log("发布求职接口返回信息");
						console.log(res);
						this.toast("已自动帮您发送求职信息");
					} else {
						this.toast(res.msg);
					}
				});
			},

			fabu() {
				let params = {
					username: this.from.username,
					idcard: this.from.idcard || "",
					province_id: this.provinces,
					city_id: this.citys,
					area_id: this.qus,
					gangwei_id: this.from.gangweiList ? this.from.gangweiList.id : "",
					zhiwu_id: this.from.zhiwuList ? this.from.zhiwuList.id : "",
					xinzirange_id: this.from.xinzirangeList ?
						this.from.xinzirangeList.id : "",
					workexperience_id: this.from.workexperienceList ?
						this.from.workexperienceList.id : "",
					mobile: this.from.mobile,
					education_id: this.from.educationList ? this.from.educationList.id : "",
					urgent_username: this.from.urgent_username,
					urgent_mobile: this.from.urgent_mobile,
					bank_name: this.bank_name,
					// bank_id: this.bank_id,
					bank_card: this.from.bank_card,
					gender: this.from.gender?.id,
					company_id: this.company_id,
					birthday: this.from.birlalal,
					dk_avatar:this.dk_avatar
				};
				console.log(params, "params");
				this.http.post("user/modify", params).then((res) => {
					if (res.code == 1) {
						this.toast("保存成功");

						this.PostToRecruitment();

						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					} else {
						this.toast(res.msg);
					}
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
	.content {
		background-color: #ffffff;
		padding: 20rpx 30rpx;
	}

	.fabu {
		position: fixed;
		bottom: 20rpx;
		width: 92%;
	}
</style>